package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.Item;
import com.zwr.sheller.core.pojo.NavigationBar;
import com.zwr.sheller.core.util.GetThreadLocal;
import com.zwr.sheller.core.util.ThreadLocalUtil;
import com.zwr.sheller.shop.mapper.ItemMapper;
import com.zwr.sheller.shop.mapper.NavigationBarMapper;
import com.zwr.sheller.shop.service.NavigationBarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 18032
 * @Date: 14:27 2022/1/10
 */
@Service
public class NavigationBarServiceImpl implements NavigationBarService {

    @Autowired(required = false)
    NavigationBarMapper navigationBarMapper;
    @Autowired(required = false)
    ItemMapper itemMapper;
    @Override
    public List<Map<String,Object>> selectAllItemName() {
        List<Map<String,Object>> list = new ArrayList<>();
        List<Item> items = itemMapper.selectAll();
        for (Item item : items) {
            Map<String,Object> map = new HashMap<>(2);
            map.put("itemId",item.getItemId());
            if (item.getItemParentId()!=null){
                Item item1 = itemMapper.selectByPrimaryKey(item.getItemParentId());
                if (item1.getItemParentId()!=null){
                    Item item2 = itemMapper.selectByPrimaryKey(item1.getItemParentId());
                    String finalItem1 = item2.getItemName()+"-"+item1.getItemName()+"-"+item.getItemName();
                    map.put("itemName",finalItem1);
                }else {
                    String finalItem = item1.getItemName()+"-"+item.getItemName();
                    map.put("itemName",finalItem);
                }
            }else {
                map.put("itemName",item.getItemName());
            }
            list.add(map);
        }
        return list;
    }

    @Override
    public Integer addBar(NavigationBar navigationBar) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        navigationBar.setStoreId(storeId);
        int insert = navigationBarMapper.insert(navigationBar);
        if (insert==0){
            throw new WQQBusinessException(WQQExceptionCode.ADD_FAILED);
        }
        return insert;
    }

    @Override
    public List<NavigationBar> selectAllBars() {
        return navigationBarMapper.selectAll();
    }

    @Override
    public Integer updateBar(NavigationBar navigationBar) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        navigationBar.setStoreId(storeId);
        int i = navigationBarMapper.updateByPrimaryKeySelective(navigationBar);
        if (i==0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }
        return i;
    }

    @Override
    public Integer deleteBar(Long barId) throws WQQBusinessException {
        int i = navigationBarMapper.deleteByPrimaryKey(barId);
        if (i == 0){
            throw  new WQQBusinessException(WQQExceptionCode.DELETE_FAILED);
        }
        return null;
    }
}
